<%@ page language="java" import="java.sql.*" import="beans.*" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<title>馆藏查询</title>
</head>
<style>

#div1{
position:absolute;
top:10%;
left:33%;
}
#content{
position:absolute;
top:20%;
left:16%;
}
</style>

<body>
<div id="div1">
查询方式:<select id="select">
<option value="book_name">书名</option>
<option value="ISNB">ISNB号</option>
<option value="author">作者</option>

</select>
&nbsp;

<input type="text" id="condition" placeholder="查询条件">
<input type="button" value="查询" id="que" onClick="query()">
<br>
</div>
<div id="content">

<%
	String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";		//得到整个文件上下文的基本路径//防止用户因为路径问题照片显示不出来
int pageSize=10;			//每页显示多少条记录
int pageNow=1;				//当前页
int rowCount=0;				//总行数
int pageCount=0;			//总页数
LibraryDao dao=new LibraryDao();
ResultSet rs=null;
Connection con=dao.con;
Statement stat=con.createStatement();
PreparedStatement ps=null;
String sPageNow=request.getParameter("pageNowOk");
if(sPageNow!=null) {
	pageNow=Integer.parseInt(sPageNow);
}
ps=con.prepareStatement("select count(*) from books");
rs=ps.executeQuery();
if(rs.next()) {
	rowCount=rs.getInt(1);
}
//out.println(rowCount);
if(rowCount%pageSize==0) {
	pageCount=rowCount/pageSize;
	
}else {
	pageCount=rowCount/pageSize+1;
}
ps=con.prepareStatement("select *from books limit "+pageSize*(pageNow-1)+","+pageSize);
rs=ps.executeQuery();

out.println("<center id='cen'>");
out.println("<table border=1>");
out.println("<tr><th>ID</th><th>书名</th><th>作者</th><th>类别</th><th>ISNB号</th><th>所在馆</th><th>描述</th><th>状态</th></tr>");
while(rs.next()) {
	out.println("<tr>");
	out.println("<td>"+rs.getString(1)+"</td>");
	out.println("<td>"+rs.getString(2)+"</td>");
	out.println("<td>"+rs.getString(3)+"</td>");
	out.println("<td>"+rs.getString(4)+"</td>");
	out.println("<td>"+rs.getString(5)+"</td>");
	out.println("<td>"+rs.getString(6)+"</td>");
	out.println("<td>"+rs.getString(7)+"</td>");
	out.println("<td>"+rs.getString(8)+"</td>");
	out.println("</tr>");
}
out.println("</table>");

if(pageNow!=1) {
	out.println("<a href="+basePath+"appearence/bookInfo.jsp?pageNowOk="+(pageNow-1)+">上一页</a>");
	//out.println("<input type='button' value='上一页' onClick=''>");
}


if(pageNow!=pageCount) {
	out.println("<a href="+basePath+"appearence/bookInfo.jsp?pageNowOk="+(pageNow+1)+">下一页</a>");
	//out.println("<input type='button' value='下一页' onClick='next()'>");
}
out.println("</center>");
%>
</div>
</body>
<script>
function query(){
	var style=document.getElementById('select').value;
	var condition=document.getElementById('condition').value;
	console.log("zhang");
	var url="servlets/QueryBook?style="+style+"&condition="+condition;
	var xml=new XMLHttpRequest();
	xml.open("GET",url,true);
	xml.onreadystatechange=function(){
		if(xml.readyState==4){
			document.getElementById("content").innerHTML=xml.responseText;
		}
	}
	xml.send();
	
}
</script>
</html>